package org.jground.rx.ui.zk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jground.rx.IRXObject;
import org.jground.rx.RXAccess;
import org.jground.rx.RXClass;
import org.jground.rx.RXDataState;
import org.jground.rx.RXLangStrings;
import org.jground.rx.RXObjectService;
import org.jground.rx.RXSystem;
import org.jground.rx.report.RXReport;
import org.jground.rx.ui.IUIStyles;
import org.jground.rx.ui.zk.report.UIReportWindow;
import org.jground.rx.web.UIProperties;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Filedownload;
import org.zkoss.zul.Menu;
import org.zkoss.zul.Menubar;
import org.zkoss.zul.Menuitem;
import org.zkoss.zul.Menupopup;

public class UINavigationPanel extends Menubar implements EventListener {

    private static Log logger = LogFactory.getLog(UINavigationPanel.class);
    private static RXLangStrings homeLS = new RXLangStrings("Search (Home)", "en", "Search (Home)", "de", "Suche (Home)");
    private static RXLangStrings newDataLS = new RXLangStrings("New Data", "en", "New Data", "de", "Neue Daten");
    private static RXLangStrings reportsLS = new RXLangStrings("Reports", "en", "Reports", "de", "Berichte");
    private static RXLangStrings helpLS = new RXLangStrings("Help", "en", "Help", "de", "Hilfe");
    private static RXLangStrings aboutLS = new RXLangStrings("About...", "en", "About...", "de", "Über...");

    private static RXLangStrings userManualLS = new RXLangStrings("User Manual", "en", "User Manual", "de", "Benutzerhandbuch");

    //
    // OBJECT LEVEL
    //

    private UIFrame uiFrame;

    private Menuitem searchMi;
    private Menuitem userManualMi;
    private Menuitem aboutMi;
    //
    private UIRecent uiRecent;

    //
    public UINavigationPanel(UIFrame uiFrame) {
        logger.debug("start init");
        this.uiFrame = uiFrame;
        this.setAutodrop(true);

        //
        // CREATE
        //

        this.setWidth("100%");
        this.setOrient("horizontal");
        //
        searchMi = new Menuitem(homeLS.toString());
        searchMi.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.search16));

        //
        Menu newModMenu = new Menu(newDataLS.toString());
        newModMenu.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.new16));
        //
        Menupopup menupopup = new Menupopup();
        menupopup.setParent(newModMenu);
        boolean hasNewData = false;

        List<RXClass> rxClasses = new ArrayList<RXClass>(RXSystem.getInstance().getRxService().getAllRXClasses());
        Collections.sort(rxClasses, new Comparator<RXClass>() {
            public int compare(RXClass o1, RXClass o2) {
                return o1.getMoid().compareTo(o2.getMoid());
            }
        });
        for (RXClass moc : rxClasses) {
            if (moc.isTopLevel() && RXAccess.getInstance().canCreate(moc)) {
                Menuitem mi = new Menuitem(moc.getName());
                // mi.setImage(UIStyles.getInstance().getImageUri(moc.getMoid(),
                // IUIStyles.S, RXDataState.NEW));
                mi.setImageContent(UIStyles.getInstance().getImage(moc.getMoid(), IUIStyles.S, RXDataState.NEW));
                mi.addEventListener(Events.ON_CLICK, this);
                mi.setAttribute("moc", moc);
                mi.setParent(menupopup);
                hasNewData = true;
            }
        }
        //
        // Report Menu
        //
        Menu reportsMenu = new Menu(reportsLS.toString());
        reportsMenu.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.report16));

        menupopup = new Menupopup();
        menupopup.setParent(reportsMenu);
        boolean hasReports = false;
        for (RXReport mor : RXSystem.getInstance().getRxService().getAllRXReports()) {
            RXClass moc = mor.getSourceRXClass();
            if (RXAccess.getInstance().canRead(moc) && RXAccess.getInstance().canRead(mor)) {
                Menuitem mi = new Menuitem(mor.getName());
                mi.setImageContent(UIStyles.getInstance().getImage(mor.getMrid(), IUIStyles.S, RXDataState.STORED));
                mi.addEventListener(Events.ON_CLICK, this);
                mi.setAttribute("mor", mor);
                mi.setParent(menupopup);
                hasReports = true;
            }
        }
        //
        // Recent
        //
        uiRecent = new UIRecent(uiFrame);
        //
        Menu helpM = new Menu(helpLS.toString());
        Menupopup helpPopup = new Menupopup();
        helpPopup.setParent(helpM);
        //
        userManualMi = new Menuitem(userManualLS.toString());
        userManualMi.addEventListener(Events.ON_CLICK, this);
        userManualMi.setParent(helpPopup);
        //
        aboutMi = new Menuitem(aboutLS.toString());
        aboutMi.addEventListener(Events.ON_CLICK, this);
        aboutMi.setImageContent(UIStyles.getInstance().getAImage(UIStyles.ImageKey.about16));
        aboutMi.setParent(helpPopup);
        //
        // LAYOUT
        //       
        searchMi.setParent(this);
        uiRecent.setParent(this);
        if (hasNewData) {
            newModMenu.setParent(this);
        }
        if (hasReports) {
            reportsMenu.setParent(this);
        }
        helpM.setParent(this);
        //
        // EVENTS
        //
        searchMi.addEventListener(Events.ON_CLICK, this);
    }

    public void onEvent(Event e) throws Exception {
        if (e.getTarget() == aboutMi) {
            UIMessageboxUtils.showInfoMessage("About", UIProperties.getInstance().getProperty(UIProperties.frame_about, "RX Method by OOIT.com AG (c) 2008"));
            return;
        }
        if (e.getTarget() == userManualMi) {
            String userManualUri = UIProperties.getInstance().getProperty(UIProperties.frame_usermanual, "");
            Filedownload.save(userManualUri, null);
            return;
        }
        //
        if (e.getTarget() == searchMi) {
            uiFrame.showHomePanel();
            // Messagebox.show("Search Button Clicked");
            return;
        }
        if (e.getTarget() instanceof Menuitem) {
            RXClass moc = (RXClass) e.getTarget().getAttribute("moc");
            if (moc != null) {
                UIEditWindow editWindow = uiFrame.getEditWindow(moc.getMoid());
                IRXObject mod = RXObjectService.getInstance().create(moc);
                editWindow.setData(mod);
                uiFrame.setMainPanel(editWindow, false);
            }
            RXReport mor = (RXReport) e.getTarget().getAttribute("mor");
            if (mor != null) {
                UIReportWindow reportWindow = uiFrame.getReportWindow(mor.getMrid());
                uiFrame.setMainPanel(reportWindow, true);
            }
            return;
        }

    }

    //
    // IMainPanel
    //

    public void BeforeClose() {

    }

    public void BeforeShow() {
    // uiFrame.SetFocusElement(this.queryTf);
    // RefreshSearch();
    }

    public Component GetView() {
        return this;
    }

    public UIFrame getUiFrame() {
        return uiFrame;
    }

}